package cn.iocoder.yudao.module.digital.controller.admin.patent;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.digital.controller.admin.patent.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.patent.PatentDO;
import cn.iocoder.yudao.module.patent.dal.dataobject.authors.AuthorsDO;
import cn.iocoder.yudao.module.digital.service.patent.PatentService;

@Tag(name = "管理后台 - 专利信息")
@RestController
@RequestMapping("/digital/patent")
@Validated
public class PatentController {

    @Resource
    private PatentService patentService;

    @PostMapping("/create")
    @Operation(summary = "创建专利信息")
    @PreAuthorize("@ss.hasPermission('digital:patent:create')")
    public CommonResult<Integer> createPatent(@Valid @RequestBody PatentSaveReqVO createReqVO) {
        return success(patentService.createPatent(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专利信息")
    @PreAuthorize("@ss.hasPermission('digital:patent:update')")
    public CommonResult<Boolean> updatePatent(@Valid @RequestBody PatentSaveReqVO updateReqVO) {
        patentService.updatePatent(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专利信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('digital:patent:delete')")
    public CommonResult<Boolean> deletePatent(@RequestParam("id") Integer id) {
        patentService.deletePatent(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专利信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('digital:patent:query')")
    public CommonResult<PatentRespVO> getPatent(@RequestParam("id") Integer id) {
        PatentDO patent = patentService.getPatent(id);
        return success(BeanUtils.toBean(patent, PatentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专利信息分页")
    @PreAuthorize("@ss.hasPermission('digital:patent:query')")
    public CommonResult<PageResult<PatentRespVO>> getPatentPage(@Valid PatentPageReqVO pageReqVO) {
        PageResult<PatentDO> pageResult = patentService.getPatentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PatentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专利信息 Excel")
    @PreAuthorize("@ss.hasPermission('digital:patent:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPatentExcel(@Valid PatentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PatentDO> list = patentService.getPatentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专利信息.xls", "数据", PatentRespVO.class,
                        BeanUtils.toBean(list, PatentRespVO.class));
    }

    // ==================== 子表（专利作者信息） ====================

    @GetMapping("/authors/list-by-patent-id")
    @Operation(summary = "获得专利作者信息列表")
    @Parameter(name = "patentId", description = "关联的专利ID，不使用外键关联")
    @PreAuthorize("@ss.hasPermission('digital:patent:query')")
    public CommonResult<List<AuthorsDO>> getAuthorsListByPatentId(@RequestParam("patentId") Integer patentId) {
        return success(patentService.getAuthorsListByPatentId(patentId));
    }

}